package my2Cents.tools;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;

import my2Cents.Bomb;
import my2Cents.Sandbox;
import my2Cents.Thruster;
import my2Cents.Undo;
import my2Cents.Util;
import my2Cents.properties.CheckBox;
import my2Cents.properties.KeyAssign;
import my2Cents.properties.Property;
import my2Cents.properties.Slider;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.FixedJoint;
import net.phys2d.raw.Joint;
import net.phys2d.raw.shapes.PCircle;

public class ThrusterTool extends AbstractBodyMaker {
	private float size = 5;
	private int timer = 0;
	private int power = 100;
	private int mult = 1;
	private boolean makeFragments;
	private int fkey = -1;
	private int rkey = -1;
	private int okey = -1;
	private boolean toggled = false;
	
	public ThrusterTool() {
		setName("Thruster");
		setDescription(
				"Click and drag to create a Thruster.\n" +
				"Dragging farther out will increase the\n" +
				"speed.\n\n" +
				"Make sure you set up the controls first."
			);
		
		Property p = new Slider("Power Multiplier",1,1,30,5,true) {
			public void valueChanged(int value) {
				mult = value;
			}
		};
		addProperty(p);
		
		KeyAssign forwardKey = new KeyAssign("Forward") {
			public void valueChanged(int value, char ch) {
				fkey = value;
			}
		};
		
		KeyAssign backwardKey = new KeyAssign("Backward") {
			public void valueChanged(int value, char ch) {
				rkey = value;
			}
		};
		
		final KeyAssign offKey = new KeyAssign("Off") {
			public void valueChanged(int value, char ch) {
				okey = value;
			}
		};
		
		addProperty(forwardKey);
		addProperty(backwardKey);
		
		CheckBox toggle = new CheckBox("Toggled",false) {
			private static final long serialVersionUID = 1L;

			public void valueChanged(boolean b) {
				toggled = b;
				if(b == true) addProperty(offKey,true);
				if(b == false) removeProperty(7,true);
			}
		};
		addProperty(toggle);
		setGhostOutlineSize(3);
	}
	
	public void startTool(MouseEvent arg0) {
		super.startTool(arg0);
		shapePos = new Vector2f(arg0.getX(), arg0.getY());
		shape = new PCircle(size);
	}
	
	public void toolThink(MouseEvent arg0) {
		super.toolThink(arg0);
		Vector2f vec = new Vector2f(arg0.getX(), arg0.getY());
		float dist = shapePos.distance(vec);
		power = (int) (dist*5);
		shapeAngle = (float) (Util.angleVec(shapePos, vec) + (180/57.3));
		// + (180/57.3)
	}
	
	private float fsin(double a) {
		return (float)Math.sin(a);
	}
	
	private float fcos(double a) {
		return (float)Math.cos(a);
	}
	
	public GeneralPath getGhostPath() {
		GeneralPath gp = super.getGhostPath();;
		gp.moveTo(shapePos.x,shapePos.y);
		
		
		double a = -shapeAngle + (90/57.3);
		
		float sx = (fcos(a) * power/5);
		float sy = (fsin(a) * power/5);
		
		gp.lineTo(shapePos.x + sy,shapePos.y + sx);
		
		a = -a - (90/57.3);
		float sx2 = (fcos(a) * 20);
		float sy2 = (fsin(a) * 20);
		
		
		
		gp.moveTo(shapePos.x, shapePos.y);
		gp.lineTo(shapePos.x + sx2, shapePos.y + sy2);
		gp.lineTo(
				(shapePos.x + sx2) - fcos(a+1) * 7, 
				(shapePos.y + sy2) - fsin(a+1) * 7);
		
		gp.moveTo(shapePos.x + sx2, shapePos.y + sy2);
		gp.lineTo(
				(shapePos.x + sx2) - fcos(a-1) * 7, 
				(shapePos.y + sy2) - fsin(a-1) * 7);
		
		gp.moveTo(shapePos.x, shapePos.y);
		gp.lineTo(shapePos.x + 10, shapePos.y);
		gp.lineTo(shapePos.x - 10, shapePos.y);
		
		gp.moveTo(shapePos.x, shapePos.y);
		gp.lineTo(shapePos.x, shapePos.y + 10);
		gp.lineTo(shapePos.x, shapePos.y - 10);
		return gp;
	}
	
	public void drawGhost(Graphics2D g) {
		super.drawGhost(g);
		
		if(this.inUse) {
			g.drawString("" + power*mult,shapePos.x - 10,shapePos.y - 40);
		}
	}
	
	public void endTool(MouseEvent arg0) {
		super.endTool(arg0);
		shape = new PCircle(size);
		Thruster t = new Thruster(shape, 20, power*mult, Sandbox.makeStatic);
		t.setMoveable(!createStatic);
		t.setPosition(shapePos.x,shapePos.y);
		t.setRotation(shapeAngle);
		t.setForwardCode(fkey);
		t.setReverseCode(rkey);
		t.setToggled(toggled);
		t.setOffCode(okey);
		sandbox.postBodyCreated(t);
		
		Util.addToS_ExcludeList(t);
		
		Body select = Util.selectBody(shapePos.x, shapePos.y, getWorld());
		Util.addToS_ExcludeList(select);
		Util.clearHandle(getWorld());
		
		Undo undo1 = new Undo(t,getWorld(),"Thruster");
		
		if(select != null && select.isMoveable() && select != t) {
			Joint j = new FixedJoint(select,t);
			getWorld().add(j);
			undo1.groupUndo(new Undo(j,getWorld(),"Thruster"));
			select.addExcludedBody(t);
			t.addExcludedBody(select);
			getWorld().clearArbiters(t);
			getWorld().clearArbiters(select);
			t.setShouldCollide(false);
		}
		Sandbox.getUndoList().add(undo1);
		Util.clearS_ExcludeList();
	}
}
